const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const GetCookieJson = data=>{//获得set-cookie的键值
  let arr=data.split(';')
  let arrJson=arr.map(e=>{
    let a=e.split('=');
    let b={}
    b[a[0].replace(/^(\s|\xA0)+|(\s|\xA0)+$/g,'')]=a[1]
    return b
  })
  return arrJson
}


//网络请求封装
const HttpRequest=(a,b={},c='get',d)=>{//四个参数（url ,发送的数据，get/post,cookie的值）
  let header={}
  if (Object.prototype.toString.call(d) === '[object String]') {
    header['content-type']="application/x-www-form-urlencoded"
    header['fcard'] = Math.round(new Date().getTime() / 1000);
    header['Cookie'] = d
  }else{
    header['content-type'] = "application/x-www-form-urlencoded"
    header['fcard'] = Math.round(new Date().getTime() / 1000);
  }
  // console.log(a,b,c,d)
  return new Promise((rep,rej)=>{
    wx.request({
      url: a,
      data: b,
      header: header,
      method: c,
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        // console.log(res)
        if (res.statusCode==200){
          rep(res)
        }else{
          rej(res)
        }
      },
      fail: function (res) {
        rej(res)
      }
    })
  })
}


function savePicToAlbum(tempFilePath) {//保存图片
  let that = this;
  wx.getSetting({
    success(res) {
      if (!res.authSetting['scope.writePhotosAlbum']) {
        wx.authorize({
          scope: 'scope.writePhotosAlbum',
          success() {
            wx.saveImageToPhotosAlbum({
              filePath: tempFilePath,
              success(res) {
                wx.showToast({
                  title: '保存成功'
                });
              },
              fail(res) {
                console.log(res);
              }
            })
          },
          fail() {
            // 用户拒绝授权,打开设置页面
            wx.openSetting({
              success: function (data) {
                console.log("openSetting: success");
              },
              fail: function (data) {
                console.log("openSetting: fail");
              }
            });
          }
        })
      } else {
        wx.saveImageToPhotosAlbum({
          filePath: tempFilePath,
          success(res) {
            wx.showToast({
              title: '保存成功',
            });
          },
          fail(res) {
            console.log(res);
          }
        })
      }
    },
    fail(res) {
      console.log(res);
    }
  })
}

function setTOut(a,b){
  setTimeout(() => {
    wx.showToast({
      title: a,
      icon: 'none',
      duration: 2000
    })
  }, b||0)
}
module.exports = {
  formatTime: formatTime,
  GetCookieJson: GetCookieJson,
  HttpRequest: HttpRequest,
  savePicToAlbum: savePicToAlbum,
  setTOut: setTOut
}
